package ar.com.angrymachine.hunter;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;

import ar.com.angrymachine.gameui.GameCanvas;
import ar.com.angrymachine.gameui.Utils;

public class GraphicStyles
{    
    private static final int ICON_WIDTH = 45;
    public static final int BORDER_MARGIN = 5;
    public static final int STATUS_BAR_ENTRY_GAP_Y = 7;
    private static final int CLIENT_OFFSET_X = 250;
    private static final int HEALTH_BAR_WIDTH = 150;
    private static final int ICON_LIST_GAP_X = 2;
    public static final int ACTIVITY_HEIGHT = 46;
    
	public static Color disappearBarColor()
	{
		return Color.BLUE;
	}

    public static Color selectedActivityColor()
	{
    	return Color.GREEN;
	}

    public static Color slumberBarColor()
    {
    	return Color.RED;
    }

    public static Color collapseBarColor()
    {
    	return Color.RED;
    }
    
    private static Color activitySeparatorColor;
    public static Color activitySeparatorColor()
    {
        if(activitySeparatorColor == null)
        {
            activitySeparatorColor = new Color(75,75,75);
        }
        return activitySeparatorColor;
    }

    public static Color buttonFillColor()
	{
    	return Color.DARK_GRAY;
    }
    
    public static Color buttonColor()
    {
    	return Color.WHITE;
    }
    
    public static Color defaultTextColor()
    {
    	return Color.WHITE;
    }
    
    private static Color textBackgroundColor;
    public static Color defaultTextBackgroundColor()
    {
        if(textBackgroundColor == null)
        {
            textBackgroundColor = new Color(12, 95, 17);
        }
        return textBackgroundColor;
    }
    
    public static void paintDefaultActivity(Graphics g, Activity activity, Rectangle area, Image icon)
	{
    	paintDefaultActivity(g, activity, area, icon, null);
	}
    
    
    public static void paintDefaultActivity(Graphics g, Activity activity, Rectangle area, Image icon, Color color)
    {
        int x = area.x + ICON_WIDTH + BORDER_MARGIN;
        int y = area.y;
        if(activity.isCompleted())
        {
            g.setColor(Color.GREEN);
        }
        else
        {
            g.setColor(Color.WHITE);
        }
        g.setFont(HunterResourceManager.activityLabelFont);
        String actionLabel = Locale.getActionLabel(activity);
        Utils.drawString(g, activity.getDescription(), x + BORDER_MARGIN,y + BORDER_MARGIN);
        int actionHeight = Utils.stringSize(g, actionLabel).y;
        
        if(activity.isCompleted())
        {
            g.setColor(Color.GRAY);
        }
        else
        {
            g.setColor(Color.GREEN);
        }
        if(color != null)
        {
        	g.setColor(color);
        	g.fillRect(area.x, area.y, area.width, area.height);
        }
        g.setFont(HunterResourceManager.smallBarFont);
        Utils.drawString(g, actionLabel, x + BORDER_MARGIN , y + BORDER_MARGIN + actionHeight);
        
        g.setColor(activitySeparatorColor());
        g.drawLine(area.x, y + area.height, area.x + area.width, y + area.height);
        
        // draw icon
        if(icon != null)
        {
            // center it
            int py = area.y + (area.height - icon.getHeight(null))/2;
            int px = area.x + (ICON_WIDTH - icon.getWidth(null))/2;
            g.drawImage(icon, px + BORDER_MARGIN, py+1, null);
        }
    }
    
    public static void paintStatusBar(Graphics g, Rectangle area)
    {
    	Utils.pushTranslation();
    	// background
        g.setColor(new Color(12, 95, 17));
        g.fillRect(area.x, area.y, area.width, area.height);
        
        // set horizontal margin
        Utils.translate(g, BORDER_MARGIN + area.x, BORDER_MARGIN + area.y);
        
        // health
        g.setColor(new Color(220, 12, 12));
        Image health = HunterResourceManager.getImage("heart-full.png");
        Point label = new Point(health.getWidth(null), health.getHeight(null));
        paintIconizedBar(g, health, health.getWidth(null) + BORDER_MARGIN, 0, 0, HEALTH_BAR_WIDTH, label.y, (int)Game.current.player.getHealth(), (int)Game.current.player.getMaxHealth());
        
        // gold
        Utils.translate(g, 0, label.y + STATUS_BAR_ENTRY_GAP_Y);
        Image gold = HunterResourceManager.getImage("gold-small.png");
        g.setColor(Color.YELLOW);
        paintIconizedLabel(g, gold,String.valueOf(Game.current.player.gold), health.getWidth(null) + BORDER_MARGIN, 0, 0);
        
        // abilities
        Utils.translate(g, 0, label.y + STATUS_BAR_ENTRY_GAP_Y);
        double[] abilityValue = 
        {
        	Game.current.player.getFightingSkill(),
        	Game.current.player.getDisarmPower(),
        	Game.current.player.getExplorationSkill(),
        	Game.current.player.getMagicSkill()
        };
        String[] abilityLabel =
        {
        	Locale.FIGHTING,
        	Locale.DISARM,
        	Locale.EXPLORATION,
        	Locale.SPELLCASTING
        };
        // find out the largest label
        int maxLabelWidth = 0;
        for (int i = 0; i < abilityLabel.length; i++)
		{
        	maxLabelWidth = Math.max(maxLabelWidth, Utils.stringSize(g, abilityLabel[i]).x);
		}
        // paint the labels
        for (int i = 0; i < abilityLabel.length; i++)
        {
	        label = Utils.stringSize(g, abilityLabel[i]);
	        paintLabeledNumber(g, abilityLabel[i], maxLabelWidth, 0, 0, (int)(abilityValue[i]*10), Color.WHITE);
	        Utils.translate(g, 0, label.y + STATUS_BAR_ENTRY_GAP_Y);
        }     
        Utils.popTranslation(g);
        // separator
        g.setColor(Color.GRAY);
        g.drawLine(area.x,area.y + area.height-1, area.x+ area.width, area.y + area.height-1);
        g.drawLine(area.x,area.y + area.height-2, area.x + area.width, area.y + area.height-2);
    }
    
    private static void paintLabeledNumber(Graphics g, String label, int labelWidth, int x, int y, int value, Color numberColor)
    {
        // label
        g.setFont(HunterResourceManager.smallBarFont);
        g.setColor(Color.WHITE);
        Utils.drawString(g, label, x, y);
        // number
        g.setColor(numberColor);
        Utils.drawString(g, String.valueOf(value), x + labelWidth + BORDER_MARGIN, y);
    }

    public static void paintLabeledIconList(Graphics g, String label, Image active, Image dead, int value, int max, int x, int y)
    {
        // label
        g.setFont(HunterResourceManager.smallBarFont);
        g.setColor(Color.GREEN);
        Utils.drawString(g, label, x, y);
        // icons
        int offsetX = Utils.stringSize(g, label).x + BORDER_MARGIN;
        for (int i=0; i != max ; i++)
        {
            Image current = i < value ? active : dead;
            g.drawImage(current, offsetX, y, null);
            offsetX += active.getWidth(null) + ICON_LIST_GAP_X;
        }

    }

    // takes the context color
    public static void paintBar(Graphics g, int x, int y, int width, int height, int value, int max)
    {
    	if(max == 0)
    		return;
        // limit values
        if(value < 0)
        {
            value = 0;
        }
        if(value > max)
        {
            value = max;
        }
        // figure out the limits
        int finalWidth = value*width/max;
        g.fillRect(x,y,finalWidth,height);
        g.setColor(g.getColor());
        g.drawLine(x,y,x,y+height-1);
        g.drawLine(x+width,y,x+width,y+height-1);
    }
    protected static final int BAR_SPLIT_GAP = 5;
    protected static final int BAR_HEIGHT = 10;
    
    public static Rectangle getBarArea(Rectangle activityArea)
    {
    	return getBarAreas(activityArea, 1)[0];
    }

    public static Rectangle[] getBarAreas(Rectangle activityArea, int bars)
    {
    	Rectangle[] areas = new Rectangle[bars];
    	Rectangle clientArea = getActivityClientArea(activityArea);
    	int defaultBarHeight = BAR_HEIGHT + BAR_SPLIT_GAP;
    	int clientSlotSize = clientArea.height/bars;
    	for (int i = 0; i < areas.length; i++)
		{
    		int posY = (i * clientSlotSize) + (clientSlotSize - defaultBarHeight)/2 + BAR_SPLIT_GAP/2;
			areas[i] = new Rectangle(clientArea.x, clientArea.y + posY, clientArea.width, BAR_HEIGHT);
		}
    	return areas;
    }
    
    public static void paintHCenteredImage(Graphics g, Image img, int x, int y)
    {
        g.drawImage(img, x - img.getWidth(null)/2, y, null);
    }
    
    public static void paintIconizedBar(Graphics g, Image icon, int gap, Rectangle area, int value, int max)
    {
    	paintIconizedBar(g, icon, gap, area.x, area.y, area.width, area.height, value, max);
    }
    
    public static void paintIconizedBar(Graphics g, Image icon, int gap, int x, int y, int width, int height, int value, int max)
    {   
        g.drawImage(icon, x, y, null);
        // center bar
        int fontHeight = icon.getHeight(null);
        int fontCenter = y + fontHeight/2;
        int barY = fontCenter - height/2; 
        paintBar(g, gap + x, barY, width - gap, height, value, max);
    }

    public static void paintIconizedLabel(Graphics g, Image icon, String label, int gap, int x, int y)
    {   
        g.drawImage(icon, x, y, null);
        Utils.drawString(g, label, gap, y);
    }
    
    public static void paintLabeledBar(Graphics g, String label, Rectangle area, int value, int max)
    {
    	paintLabeledBar(g, label, Color.WHITE, Utils.stringSize(g, label).x, area.x, area.y, area.width, area.height, value, max);
    }
    
    public static void paintLabeledBar(Graphics g, String label, int labelGap, int x, int y, int width, int height, int value, int max)
    {
    	paintLabeledBar(g, label, Color.WHITE, labelGap, x, y, width, height, value, max);
    }
    
    private static void paintLabeledBar(Graphics g, String label, Color labelColor, int labelGap, int x, int y, int width, int height, int value, int max)
    {
    	// context
        int labelWidth = labelGap + 3;
        int fontHeight = g.getFontMetrics().getHeight();
        Color barColor = g.getColor();
        g.setColor(labelColor);
        g.setFont(HunterResourceManager.smallBarFont);

        // find the center of this widget
        int highestHeight = Math.max(height, fontHeight);
        int centerY = y + highestHeight/2;
        
        // draw the label
        Utils.drawString(g, label, x, centerY - fontHeight/2);
        // draw the bar
        g.setColor(barColor);
        paintBar(g, labelWidth + x, centerY - height/2, width - labelWidth, height, value, max);        
    }

    public static Rectangle getActivityClientArea(Rectangle activityArea)
    {
        int offset = CLIENT_OFFSET_X;
        return new Rectangle(activityArea.x + offset,
                             activityArea.y + BORDER_MARGIN,   
                             activityArea.width - offset - BORDER_MARGIN - GameCanvas.screen.theme.getDefaultBarWidth(),
                             activityArea.height - BORDER_MARGIN*2);
    }

}
